/*
 * XMLSchemaLoader.java
 * Created on 10.08.2005
 *
 * VirtualHockey.net Internet Platform
 * Copyright (c) 2005-2006, Jan Janke
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hockeo.vhbase.utils.xml;

import java.io.IOException;

import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * Shall be used to load XML schemata using the Resource utility classes from the Spring
 * framework. See the Spring documentation for further information on how to specify a
 * file path so that it can be loaded by using a {@link ResourceLoader}.
 * 
 * @version $Id$
 * @author jankejan
 */
public final class GXmlSchemaLoader implements EntityResolver, ResourceLoaderAware
{
  /*
   * ================================================================================
   * Private members
   * ================================================================================
   */
  private ResourceLoader d_resourceLoader;
  
  /*
   * ================================================================================
   * Non-static methods
   * ================================================================================
   */

  /*
   * (non-Javadoc)
   * 
   * @see org.springframework.context.ResourceLoaderAware#setResourceLoader(org.springframework.core.io.ResourceLoader)
   */
  public void setResourceLoader( ResourceLoader resourceLoader )
  {
    d_resourceLoader = resourceLoader;
  }

  /**
   * It is assumed that the system id points to the XML schema that should be loaded
   * and be returned as {@link InputSource}. The public id is ignored.
   * 
   * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String, java.lang.String)
   */
  public InputSource resolveEntity( String strPublicId, String strSystemId ) throws SAXException, IOException
  {
    Resource resSchema = d_resourceLoader.getResource( strSystemId );

    return new InputSource( resSchema.getInputStream() );
  }
}
